/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.gwc.yy.liuxue.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.gwc.yy.liuxue.entity.School;
import com.gwc.yy.liuxue.dao.SchoolDao;
import com.gwc.yy.liuxue.entity.ProfIntro;
import com.gwc.yy.liuxue.dao.ProfIntroDao;

/**
 * 院校Service
 * @author 黎俊涛
 * @version 2017-07-05
 */
@Service
@Transactional(readOnly = true)
public class SchoolService extends CrudService<SchoolDao, School> {

	@Autowired
	private ProfIntroDao profIntroDao;
	
	public School get(String id) {
		School school = super.get(id);
		school.setProfIntroList(profIntroDao.findList(new ProfIntro(school)));
		return school;
	}
	
	public ProfIntro getProfIntro(String id){
		ProfIntro prof=profIntroDao.get(id);
		return prof;
	}
	
	public List<School> findList(School school) {
		return super.findList(school);
	}
	
	public Page<School> findPage(Page<School> page, School school) {
		Page<School> result = super.findPage(page, school);
		for(School s : result.getList()){
			s.setProfIntroList(profIntroDao.findList(new ProfIntro(s)));
		}
		return result;
	}
	
	@Transactional(readOnly = false)
	public void save(School school) {
		super.save(school);
		for (ProfIntro profIntro : school.getProfIntroList()){
			if (profIntro.getId() == null){
				continue;
			}
			if (ProfIntro.DEL_FLAG_NORMAL.equals(profIntro.getDelFlag())){
				if (StringUtils.isBlank(profIntro.getId())){
					profIntro.setSchool(school);
					profIntro.preInsert();
					profIntroDao.insert(profIntro);
				}else{
					profIntro.preUpdate();
					profIntroDao.update(profIntro);
				}
			}else{
				profIntroDao.delete(profIntro);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(School school) {
		super.delete(school);
		profIntroDao.delete(new ProfIntro(school));
	}
	
}